import subprocess


class ExternalTextProcessor(object):

    def __init__(self, cmd):
        self.cmd = cmd

    def process(self, input_text):
        proc = subprocess.Popen(self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        outs, errs = proc.communicate(input=bytes(input_text, encoding='utf-8'))

        return outs.decode('utf-8')


def split_mose_en(content):
    proc_sent = ExternalTextProcessor(
        'C:/perl/perl/bin/perl.exe moses/ems/support/split-sentences.perl -l en -a -b -q')  # windows
    # proc_sent = ExternalTextProcessor('moses/ems/support/split-sentences.perl -l en -a -b -q'.split())  # linux
    tokenized_segments = proc_sent.process(content).strip()
    return tokenized_segments.split("\n")


def split_mose_zh(content):
    proc_sent = ExternalTextProcessor(
        'C:/perl/perl/bin/perl.exe moses/ems/support/split-sentences.perl -l zh -a -b -q')  # windows
    # proc_sent = ExternalTextProcessor('moses/ems/support/split-sentences.perl -l zh -a -b -q'.split())  # linux
    tokenized_segments = proc_sent.process(content).strip()
    return tokenized_segments.split("\n")


if __name__ == '__main__':
    pass
    # print(split_mose_en("Hello. Hello,are you here?"))
    # print(split_mose_zh("我就是试试你的膝跳反射。中文输入法真难用啊。你就是这么想的吗？"))

